import time

import unittest
# from unittest import TestCase
from selenium import webdriver
from selenium.webdriver.common.by import By
from ddt import ddt, data, unpack
from method import method
from _init_ import TestCaseOne

da1 = [
    ["wangwu", 123456, 123456]
]
da2 = [
    ["wangwu1", 123456, 123456]
]
da3 = [
    ["wangwu", 123456, 1234567]
]
da4 = [
    ["wangwu", 123456]
]


# 选择跳过的方法
def chooseSkip(get_skip_or_no_skip, get_choose_case):
    all_test = []
    for i in range(1, 5):
        all_test.append(i)
    for j in get_choose_case:
        all_test.remove(j)
    if get_skip_or_no_skip == 1:
        the_choose_skip = all_test
    else:
        the_choose_skip = get_choose_case
    return the_choose_skip


# 选择跳过这些用例则选择0，选择不跳过这些用例则选择1
skip_or_no_skip = 0
choose_case = []
choose_skip = chooseSkip(skip_or_no_skip, choose_case)
s = method()
# 创建一个列表用来存储选择，有多少方法就准备多少元素
skip_test = s.skipTest(4, choose_skip)


@ddt
class TestToregister(TestCaseOne):
    @unittest.skipIf(skip_test[0] == 1, "暂时跳过")
    @data(*da1)
    @unpack
    def test_001_torSuccess(self, a, b, c):
        # 清理数据库中数据，保证数据库中没有用例数据
        s.cleanDatabaseByUsername(a)

        # 创建谷歌浏览器对象
        chromeDriver = webdriver.Chrome()

        # 打开网页
        chromeDriver.get("http://localhost:90/")

        # 窗口最大化
        chromeDriver.maximize_window()

        # 点击注册链接
        chromeDriver.find_element(By.XPATH, "//a[@href='/toregister.html']").click()

        time.sleep(1)

        # 输入用户名
        chromeDriver.find_element(By.ID, "username").send_keys(a)

        # 输入密码
        chromeDriver.find_element(By.ID, "password").send_keys(b)

        # 输入重复密码
        chromeDriver.find_element(By.ID, "repassword").send_keys(c)

        time.sleep(1)
        # 点击注册
        chromeDriver.find_element(By.CSS_SELECTOR, "#login_btn").click()
        time.sleep(3)

        # 获取界面路径
        result1 = str(chromeDriver.current_url)
        # 查询数据库中姓名为用例姓名的数据,并获取查询返回的元组长度
        result2 = len(s.findUsername(a))
        flag = True
        # 判断是否返回主界面且数据库增加数据
        if result1 == "http://localhost:90/" and result2 == 1:
            print("用例通过")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_001_torSuccess")
            chromeDriver.save_screenshot(screenshot)
            flag = False

        time.sleep(3)
        self.assertEqual(True, flag)
        # 退出浏览器
        chromeDriver.quit()

    @unittest.skipIf(skip_test[1] == 1, "暂时跳过")
    @data(*da2)
    @unpack
    def test_002_torFailUserExists(self, a, b, c):
        # 查询数据库中是否有测试姓名
        p = len(s.findUsername(a))
        # 如果没有则添加测试姓名，有的话则清理测试姓名再添加测试姓名
        if p == 0:
            s.addUsername(a)
        else:
            s.cleanDatabaseByUsername(a)
            s.addUsername(a)
        # 创建谷歌浏览器对象
        chromeDriver = webdriver.Chrome()

        # 打开网页
        chromeDriver.get("http://localhost:90/")

        # 窗口最大化
        chromeDriver.maximize_window()

        # 点击注册链接
        chromeDriver.find_element(By.XPATH, "//a[@href='/toregister.html']").click()

        time.sleep(1)

        # 输入用户名
        chromeDriver.find_element(By.ID, "username").send_keys(a)

        # 输入密码
        chromeDriver.find_element(By.ID, "password").send_keys(b)

        # 输入重复密码
        chromeDriver.find_element(By.ID, "repassword").send_keys(c)

        time.sleep(1)
        # 点击注册
        chromeDriver.find_element(By.CSS_SELECTOR, "#login_btn").click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = chromeDriver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[0].text
        # 预期结果
        expect = ("用户%s已存在" % a)
        if result == expect:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed('test_002_torFailUserExists')
            chromeDriver.save_screenshot(screenshot)
        time.sleep(3)
        self.assertEqual(expect, result)
        # 退出浏览器
        chromeDriver.quit()

    @unittest.skipIf(skip_test[2] == 1, "暂时跳过")
    @data(*da3)
    @unpack
    def test_003_torFailDiffPassword(self, a, b, c):
        # 清理数据库中数据，保证数据库中没有用例数据
        s.cleanDatabaseByUsername(a)
        # 创建谷歌浏览器对象
        chromeDriver = webdriver.Chrome()

        # 打开网页
        chromeDriver.get("http://localhost:90/")

        # 窗口最大化
        chromeDriver.maximize_window()

        # 点击注册链接
        chromeDriver.find_element(By.XPATH, "//a[@href='/toregister.html']").click()

        time.sleep(1)

        # 输入用户名
        chromeDriver.find_element(By.ID, "username").send_keys(a)

        # 输入密码
        chromeDriver.find_element(By.ID, "password").send_keys(b)

        # 输入重复密码
        chromeDriver.find_element(By.ID, "repassword").send_keys(c)

        time.sleep(1)
        # 点击注册
        chromeDriver.find_element(By.CSS_SELECTOR, "#login_btn").click()
        time.sleep(3)

        #  获取实际标题结果
        result = ""
        #  判断是否在注册页面，避免获取不到元素报错
        if str(chromeDriver.current_url) == "http://localhost:90/toregister.html":
            resultall = chromeDriver.find_elements(By.CLASS_NAME, "help-block")
            result = resultall[2].text
        # 预期结果
        expect = "两次密码不相同"
        if result == expect:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed('test_003_torFailDiffPassword')
            chromeDriver.save_screenshot(screenshot)
        time.sleep(3)
        self.assertEqual(expect, result)
        # 退出浏览器
        chromeDriver.quit()

    @unittest.skipIf(skip_test[3] == 1, "暂时跳过")
    @data(*da4)
    @unpack
    def test_004_torFailNoRePassword(self, a, b):
        # 清理数据库中数据，保证数据库中没有用例数据
        s.cleanDatabaseByUsername(a)
        # 创建谷歌浏览器对象
        chromeDriver = webdriver.Chrome()

        # 打开网页
        chromeDriver.get("http://localhost:90/")

        # 窗口最大化
        chromeDriver.maximize_window()

        # 点击注册链接
        chromeDriver.find_element(By.XPATH, "//a[@href='/toregister.html']").click()

        time.sleep(1)

        # 输入用户名
        chromeDriver.find_element(By.ID, "username").send_keys(a)

        # 输入密码
        chromeDriver.find_element(By.ID, "password").send_keys(b)

        time.sleep(1)
        # 点击注册
        chromeDriver.find_element(By.CSS_SELECTOR, "#login_btn").click()
        time.sleep(3)

        #  获取实际标题结果
        resultall = chromeDriver.find_elements(By.CLASS_NAME, "help-block")
        result = resultall[2].text
        # 预期结果
        expect = "重复密码为空"
        if result == expect:
            print("用例通过！")
        else:
            print("用例不通过！")
            screenshot = s.failedScreenshotNamed("test_004_torFailNoRePassword")
            chromeDriver.save_screenshot(screenshot)
        time.sleep(3)
        self.assertEqual(expect, result)
        # 退出浏览器
        chromeDriver.quit()
